$(document).ready(function () {



    layui.use(function () {

        var layer = layui.layer;
        workTaskCount()
        entryMenu()
        queryDuty()
        seleteDutyList()
    });
})


function queryDuty() {

    new AjaxChain()
        .setUrl('/org/duty/info')
        .then(data => {

            if (data) {
                $('#duty1').html(data.officerName)
                $('#duty2').html(data.monitorName)
            }

        })
        .catch(err => {
            console.error('请求异常:', err);
        })
        .send();

}



function seleteDutyList() {
    // 干部
    new AjaxChain()
        .setUrl('/person/officer/select')
        .then(data => {

            var list = [];

            data.map(item => {
                list.push(
                    {
                        title: item.name,
                        id: item.id
                    }
                )
            })

            layui.dropdown.render({
                elem: '#duty1',
                data: list,
                click: function (obj) {

                    changeDutyPerson(
                        {
                            officerId: obj.id
                        }
                    )

                }

            });

        })
        .catch(err => {
            console.error('请求异常:', err);
        })
        .send();


    // 班长
    new AjaxChain()
        .setUrl('/person/monitor/select')
        .then(data => {

            var list = [];

            data.map(item => {
                list.push(
                    {
                        title: item.name,
                        id: item.id
                    }
                )
            })

            layui.dropdown.render({
                elem: '#duty2',
                data: list,
                click: function (obj) {

                    changeDutyPerson(
                        {
                            monitorId: obj.id
                        }
                    )

                }

            });

        })
        .catch(err => {
            console.error('请求异常:', err);
        })
        .send();

}


// 切换值班人员
function changeDutyPerson(res) {


    new AjaxChain()
        .setUrl('/org/duty/save')
        .setMethod('post')
        .setData(JSON.stringify(res))
        .then(data => {

            layer.msg('切换成功')
            queryDuty()
        })
        .catch(err => {
            console.error('请求异常:', err);
        })
        .send();
}